home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++,
- Subject: Re: Tough FACTORIAL math problem...
- Date: 15 Feb 1996 17:08:15 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4fvp9v$sso@newsroom.hitc.com>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <31224679.6193@born.com>, clelaj@born.com says...
- >
- >The Crow wrote:
- >>
- >> Here is what I am trying to do, I want to find the last NON-ZERO digit
- > of a
- >> given factorial. For instance,
- >>
- >> 5! = 120
- >>
- >> so the last non-zero digit is 2. I want to be able to do this up to 1
- >000.
- [SNIP!!!]
-
- Here are two other approaches, which should take up very little space and
- be lightning quick:
-
- Since you only care about the smallest non-zero digit, just multiply the
- last smallest non-zero digit by the next number's smallest non-zero
- digit!
- You can easily test for the result being a multiple of 10. This requires
- a bit of bookkeeping for dealing with numbers like 10, 100,
- 200, 350, etc. but that's fairly easy to handle...Heck, you could write
- three nested for(...) loops to handle going from 1-1000, although some
- purists will balk...
-
- If you want to minimize the testing for zero-digits, build yourself a 9x9
- matrix which holds the least-significant digit of the product of
- (i+1)*(j+1)
- [i,j are C/C++ indexes ranging from 0-8]. You then can use three nested
- for(...) loops and just select the row (new number lsd) and column (lsd
- of previous factorial) and look it up...
-
- The nice thing with the second approach is that it can be easily modified
- to do this in MOD J, where J is not 10. Just adjust the for(...) loops
- to range between 0 and J-1 and have the matrix be (J-1)x(J-1) big...You
- just pay for the looping and lookup time, but you don't have to multiply
- things and divide...
-
- Hope this helps...
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-